#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 2e5+10;
const int inf = 0x3f3f3f3f;

int n,k,a,b,c,v[110][110],h[N],incf[N],pre[N],S,T,cnt = 1;
ll cost,dis[N],maxflow;
bool vis[N];
struct edge{int v,w,c,nxt;}e[N<<4];
void link(int x,int y,int w,int c){e[++cnt] = (edge){y,w,c,h[x]};h[x] = cnt;e[++cnt] = (edge){x,0,-c,h[y]};h[y] = cnt;}

bool spfa(){
	memset(vis,0,sizeof vis); memset(dis,0x3f,sizeof dis); dis[S] = 0; vis[S] = 1; queue<int> q; q.push(S); incf[S] = inf;
	while(q.size()){
		int x = q.front(); q.pop();
		for(int i = h[x],y;i;i = e[i].nxt){
			if(dis[y = e[i].v] > dis[x] + e[i].c && e[i].w){
				dis[y] = dis[x] + e[i].c; pre[y] = i; incf[y] = min(incf[x],e[i].w); if(!vis[y]) vis[y] = 1,q.push(y);
			}
		}vis[x] = 0;
	}return dis[T] < inf;
}

void update(int x = T){
	while(x ^ S) {int i = pre[x]; e[i].w -= incf[T]; e[i ^ 1].w += incf[T]; x = e[i ^ 1].v;}
	cost += incf[T] * dis[T]; maxflow += incf[T];
}

int id(int i,int j,int fl){return (fl - 1) * n * n + (i - 1) * n + j;}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in,k = in,a = in,b = in,c = in;
	for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) v[i][j] = in;
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++){
			for(int d = 1;d <= k + 1;d++){
				int nid = id(i,j,d);
				if(!v[i][j] && d <= k || d == 1){
					if(i + 1 <= n) link(nid,id(i + 1,j,d + 1),inf,0);
					if(j + 1 <= n) link(nid,id(i,j + 1,d + 1),inf,0);
					if(i > 1) link(nid,id(i - 1,j,d + 1),inf,b);
					if(j > 1) link(nid,id(i,j - 1,d + 1),inf,b);
				}
				if(d == 1) continue;
				if(!v[i][j]) link(nid,id(i,j,1),1,a+c);
				if(v[i][j]) link(nid,id(i,j,1),inf,a);
			}
		}
	S = 0; link(S,id(1,1,1),1,0); T = n * n * (k + 1) + 1; for(int i = 1;i <= k + 1;i++) link(id(n,n,i),T,1,0);
	while(spfa()) update();
	printf("%lld\n",cost);
	return 0;
}

